/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","ojs/ojcomponentcore","ojs/ojpopupcore"],function(a,g){(function(){var b="oj-left oj-center oj-right oj-top oj-middle oj-bottom".split(" "),c={"right-top":"oj-right oj-top","right-middle":"oj-right oj-middle","right-bottom":"oj-right oj-bottom","left-top":"oj-left oj-top","left-middle":"oj-left oj-middle","left-bottom":"oj-left oj-bottom","center-top":"oj-center oj-top","center-middle":"oj-left oj-middle","center-bottom":"oj-center oj-bottom"};a.Ra("oj.ojPopup",g.oj.baseComponent,
{widgetEventPrefix:"oj",options:{autoDismiss:"focusLoss",chrome:"default",initialFocus:"auto",position:{my:"start top",at:"start bottom",of:"",collision:"flip"},tail:"none",modality:"modeless",role:"tooltip",beforeOpen:null,open:null,beforeClose:null,close:null,focus:null},_ComponentCreate:function(){this._super();var a;this.oe?a=g(this.oe):(a=g("\x3cdiv\x3e"),this.element.after(a));this.kg=a.hide().addClass("oj-popup").attr("aria-hidden","true");a.addClass("oj-component");var b=g("\x3cdiv\x3e").addClass("oj-popup-content");
b.attr("role","presentation");b.appendTo(a);this.element.appendTo(b);this.element.show();this.Z5();this.Uca();this.cF=g.proxy(this.dF,this)},_destroy:function(){this.isOpen()&&this.hv();this.w6();delete this.cF;delete this.wn;a.D.unwrap(this.element,this.kg);this.element.hide();var b=this.xs;isNaN(b)||(delete this.xs,window.clearTimeout(b));this.x6();this._super()},widget:function(){return this.kg},open:function(b,c){if(this.isOpen()&&(this.close(),this.isOpen()))return;this.XDa(b);var f=this.kg;
b=this.Jd;a.cb.hm(f.attr("id"))&&f.attr("id",this.SJ("wrapper"));if(!1!==this._trigger("beforeOpen")){this.jda(c);var g=this.options;this.zW(g.autoDismiss);this.qpa();f.attr("role",g.role);c=g.position;var k="rtl"===this.nd();c=a.Ed.ml(c,k);var k="oj-popup-layer",l=g.tail;"none"!==l&&(k+=" "+["oj-popup-tail",l].join("-"));l={};l[a.U.qb.Ih]=f;l[a.U.qb.zB]=b;l[a.U.qb.DB]=c;l[a.U.qb.vu]=this.Vy();l[a.U.qb.Ur]=k;l[a.U.qb.Ak]=g.modality;a.U.Qe().open(l);this._trigger("open");this.bV();this.Xya();this._on(f,
{keydown:this.oz,keyup:this.oz});b&&0<b.length&&this._on(b,{keydown:this.oz,keyup:this.oz})}},close:function(){if(this.isOpen()&&(!1!==this._trigger("beforeClose")||this.ED)){this.aDa();var b=this.Jd,c=this.kg;this._off(c,"keydown keyup");b&&0<b.length&&this._off(b,"keydown keyup");this.x6();b={};b[a.U.qb.Ih]=c;a.U.Qe().close(b);this.pCa();this.zW();delete this.Jd;c=this.options.position;c._ofo&&(delete c._ofo,delete c.of);this._trigger("close")}},isOpen:function(){return this.kg.is(":visible")},
refresh:function(){this._super();this.isOpen()&&this.nca();var b=this.kg;a.U.Qe().wO(b,a.U.Dd.$p)},_setOption:function(b,c){var f=this.options;switch(b){case "tail":c!==f.tail&&this.jEa(c);break;case "chrome":c!==f.chrome&&this.Uca(c);break;case "position":this.jda(c);this.refresh();return;case "autoDismiss":this.isOpen()&&c!==f.autoDismiss&&this.zW(c);break;case "modality":this.isOpen()&&(f={},f[a.U.qb.Ih]=this.kg,f[a.U.qb.Ak]=c,a.U.Qe().aA(f))}this._superApply(arguments)},dNa:function(){return"oj-popup"},
jEa:function(a){this.w6();this.Z5(a);this.nca()},Z5:function(b){b=b?b:this.options.tail;if("none"!==b){b=["oj-popup-tail",b].join("-");var c=g("\x3cdiv\x3e").hide();c.addClass("oj-popup-tail").addClass(b);c.attr("role","presentation");this.cea=c.attr("id",this.SJ("tail")).attr("id");var f=this.kg;c.appendTo(f);f.addClass(b);this.isOpen()&&(c={},c[a.U.qb.Ih]=f,c[a.U.qb.Ur]="oj-popup-layer "+b,a.U.Qe().aA(c))}},X8:function(){var a=this.cea;return a?g(document.getElementById(a)):null},w6:function(){var b=
this.X8();b&&b.remove();delete this.cea;b=this.kg;b.removeClass(["oj-popup-tail",this.options.tail].join("-"));if(this.isOpen()){var c={};c[a.U.qb.Ih]=b;c[a.U.qb.Ur]="oj-popup-layer";a.U.Qe().aA(c)}},Uca:function(a){a=a?a:this.options.chrome;var b=this.kg;"default"===a&&b.hasClass("oj-popup-no-chrome")?b.removeClass("oj-popup-no-chrome"):"none"!==a||b.hasClass("oj-popup-no-chrome")||b.addClass("oj-popup-no-chrome")},XDa:function(b){b?"string"===g.type(b)?b=g(b):1===b.nodeType&&(b=g(b)):b=g(document.activeElement);
if(b instanceof g&&1<b.length)for(var c=this.kg,f=0;f<b.length;f++){var h=b[0];if(!a.D.Gt(c[0],h)){b=g(h);break}}else if(!(b instanceof g)||b instanceof g&&0===b.length)b=g(document.activeElement);this.Jd=b},jda:function(a){var b=this.options;a&&(b.position=g.extend(b[a],a));a=b.position;b=this.cF;g.isFunction(a.using)&&a.using!==b&&(a.origUsing=a.using);a.using=b;b=this.Jd;a.of||(a.of=b,a._ofo=!0)},dF:function(d,e){var f=e.element.element;if(d.top!==f.css("top")||d.left!==f.css("left")){var h=this.X8();
if(h){h.hide();for(var k=0;k<b.length;k++)h.removeClass(b[k]),f.removeClass(b[k]);h.removeAttr("style");if(e.target&&0===e.target.height&&0===e.target.width&&(k="rtl"===this.nd(),k=a.Ed.ml(this.options.position,k).my,!a.cb.hm(k))){var k=k.split(" "),l="center"===k[0]?k[0]:e.horizontal,m="middle";1<k.length&&(m="center"===k[1]?"middle":k[1]);e.horizontal=l;e.vertical=m}k=c[[e.horizontal,e.vertical].join("-")];h.addClass(k);f.addClass(k);h.show();"left"===e.horizontal?(k=h.outerWidth(),k-=k+a.D.hr(h.css("left")),
d.left+=k-2):"right"===e.horizontal&&(k=h.outerWidth(),k-=k+a.D.hr(h.css("right")),d.left-=k-2);"top"===e.vertical?(k=h.outerHeight(),k-=k+a.D.hr(h.css(e.vertical)),d.top+=k-2):"bottom"===e.vertical&&(k=h.outerHeight(),k-=k+a.D.hr(h.css(e.vertical)),d.top-=k-2);f.css(d);"center"===e.horizontal&&"middle"!==e.vertical?(f=f.width(),f=Math.round((f/2-h.outerWidth()/2)/f*100),h.css({left:f+"%"})):"middle"===e.vertical&&(f=f.height(),f=Math.round((f/2-h.outerHeight()/2)/f*100),h.css({top:f+"%"}))}else f.css(d);
h=this.options;(f=h.position.origUsing)&&f(d,e);"focusLoss"===h.autoDismiss&&a.Ed.pZ(e)&&(this.UU=!0,this.xs=this._delay(g.proxy(this.hv,this),1))}},nca:function(){var b=this.kg,c=this.options.position,f="rtl"===this.nd();b.position(a.Ed.ml(c,f))},bV:function(b){var c=this.Nsa();b&&"none"===c&&(c="popup");if("firstFocusable"===c){b=this.element.find(":focusable");for(var f,g=0;g<b.length;g++)if(a.me.EA(b[g])){f=b[g];break}f?(f.focus(),this._trigger("focus")):c="popup"}"popup"===c&&(c=this.kg,c.attr("tabindex",
"-1"),c.focus(),this._trigger("focus"))},Nsa:function(){var b=this.options,c=b.initialFocus;"auto"===c&&(c="modal"===b.modality?a.D.jf()?"popup":"firstFocusable":"none");return c},uK:function(b,c){b||(b=document.activeElement);if(!b)return!1;var f=this.kg;c&&(f=f.parent());return a.D.Gt(f[0],b)},N$:function(b){b||(b=document.activeElement);return a.D.Gt(this.Jd[0],b)},aDa:function(){this.UU?delete this.UU:this.uK(null,!0)&&this.Jd.focus()},oz:function(a){if(!a.isDefaultPrevented()){var b=a.type,c=
a.target;if("keyup"===b&&a.keyCode===g.ui.keyCode.ESCAPE&&(this.uK(c)||this.N$(c)))a.preventDefault(),this.close();else if("keydown"===b&&117===a.keyCode)this.uK(c)?(c=this.options,"modeless"===c.modality?(a.preventDefault(),a=this.Jd,a.focus()):this.close()):this.N$(c)&&(a.preventDefault(),this.bV(!0));else if("keydown"===b&&a.keyCode===g.ui.keyCode.TAB&&this.uK(c)){var h=this.element.find(":tabbable");if(0<h.length){var b=h[0],h=h[h.length-1],k=this.kg;b!==c&&k[0]!==c||!a.shiftKey?h!==c||a.shiftKey||
(a.preventDefault(),h===b?(k.attr("tabindex","-1"),k.focus()):g(b).focus()):(a.preventDefault(),b===h&&b===c?(k.attr("tabindex","-1"),k.focus()):g(h).focus())}else a.preventDefault(),c=this.options,"modeless"===c.modality?(a=this.Jd,a.focus()):this.close()}}},zW:function(b){var c=this.o7,f=this.Vy();c&&(delete f[a.U.Dd.Hx],delete this.o7);"focusLoss"===b&&(c=this.o7=g.proxy(this.Usa,this),f[a.U.Dd.Hx]=c);this.isOpen()&&(b={},b[a.U.qb.Ih]=this.kg,b[a.U.qb.vu]=f,a.U.Qe().aA(b))},Usa:function(b){var c=
this.Jd,f=this.kg.parent(),g=b.target,k=this.gJ;if(k&&(k=k.getLink())&&a.D.Gt(k[0],g))return;if(!a.D.Gt(c[0],g)&&!a.D.Gt(f[0],g)){if(a.me.EA(g)){if("mousedown"===b.type||"touchstart"===b.type)return;this.UU=!0}this.close()}},qpa:function(){var a=this.Jd,b=this.kg.attr("id"),c=a.attr("aria-describedby"),c=c?c.split(/\s+/):[];c.push(b);c=g.trim(c.join(" "));a.attr("aria-describedby",c)},pCa:function(){var a=this.Jd,b=this.kg.attr("id"),c=a.attr("aria-describedby"),c=c?c.split(/\s+/):[],b=g.inArray(b,
c);-1!==b&&c.splice(b,1);(c=g.trim(c.join(" ")))?a.attr("aria-describedby",c):a.removeAttr("aria-describedby")},Xya:function(){var b=a.Oa.er().os===a.Oa.yl.fP,c=this.uV;c||(c=this.uV=new a.Cg);var f;f=b?this.F("none"===this.options.initialFocus?"ariaLiveRegionInitialFocusNoneTouch":"ariaLiveRegionInitialFocusFirstFocusableTouch"):this.F("none"===this.options.initialFocus?"ariaLiveRegionInitialFocusNone":"ariaLiveRegionInitialFocusFirstFocusable");c.eGa(f);if(b){if(!this.gJ){var c=this.SJ("focusSkipLink"),
h=this.Jd,b=g.proxy(this.bV,this,!0);f=this.F("ariaFocusSkipLink");this.gJ=new a.Al(h,f,b,c)}this.bS||(c=this.SJ("closeSkipLink"),h=this.element,b=g.proxy(this.hv,this),f=this.F("ariaCloseSkipLink"),this.bS=new a.Al(h,f,b,c))}},x6:function(){var a=this.uV;a&&(a.destroy(),delete this.uV);if(a=this.gJ)a.destroy(),delete this.gJ;if(a=this.bS)a.destroy(),delete this.bS},SJ:function(b){var c=this.element.attr("id");a.cb.hm(c)&&(c=this.uuid);return[c,b].join("_")},tw:function(){this.element.remove()},Vy:function(){if(!this.wn){var b=
this.wn={};b[a.U.Dd.Ix]=g.proxy(this.hv,this);b[a.U.Dd.Jx]=g.proxy(this.tw,this);b[a.U.Dd.$p]=g.proxy(this.refresh,this)}return this.wn},hv:function(){this.ED=!0;this.close();delete this.ED}});a.Components.Wn({ojPopup:{modality:a.Components.Ne(function(){return(a.Wc.De("oj-popup-option-defaults")||{}).modality})}})})();a.Components.Xa("ojPopup","baseComponent",{properties:{autoDismiss:{type:"string"},chrome:{type:"string"},initialFocus:{type:"string"},modality:{type:"string"},widget:{},position:{type:"Object"},
role:{type:"string"},tail:{type:"string"}},methods:{close:{},isOpen:{},open:{},refresh:{}},extension:{_hasWrapper:!0,_innerElement:"div",_widgetName:"ojPopup"}});a.Components.register("oj-popup",a.Components.getMetadata("ojPopup"))});